//
//  LoadingController.swift
//  BJ Racing -Guild wars velocity
//
//  Created by inphase on 17/9/22.
//  Copyright © 2017年 inphase. All rights reserved.
//

import UIKit
import LeanCloud

class LoadingController: UIViewController {
    
    
    //类似于OC中的typedef
    typealias sendValueClosure=(_ str:String)->Void

    //声明一个闭包
    var testClosure:sendValueClosure?
   
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
     self.view.addSubview(bgImgView())
      
    self.httpForRequest()
    self.LeanClouds()
    
    }
   
    // MARK:储存数据
    func LeanClouds() {
        
        LeanCloud.initialize(applicationID: "idVwLSxUhIRI5j17tXvAWtl7-gzGzoHsz", applicationKey: "o8XritBApHOAya1z99jQJoCz")
    
        
    }


     // MARK:页面跳转
    
    func httpForRequest() {
        
        let query = LCQuery(className: "TodoFolder")
        
        query.get("59ca6e7317d0090063800f1c") { result in
            switch result {
            case .success(let todo):
                // 使用 get 方法访问非预定义属性
                let url    = todo.get("WoCaoNiu") as! LCString
              
                let UrlStr = url.value
                
                
                self.showViewStr(UrlStr)
                    
          
                
            case .failure(let error):
                print(error)
                  self.showPView()
            }
        }
    }
    
    
     // MARK:页面Tow
    func showViewStr(_ UrlStr:String?) {
        let vc :WeViewController = WeViewController()
        
        vc.UrlStr = UrlStr
        
        UIApplication.shared.keyWindow?.rootViewController=vc
        
        
           }
    
     // MARK:页面One
    func showPView() {
        
//        let  vc:ViewController = ViewController()
        let rootViewController = UIStoryboard(name: "Main", bundle: nil)
            .instantiateInitialViewController()! as UIViewController
        UIApplication.shared.keyWindow?.rootViewController=rootViewController
        
        self.bgImgView().removeFromSuperview()
    }
    
    
    func bgImgView()->UIImageView {
        
        let W:CGFloat = 50
        let X:CGFloat = (self.view.frame.size.width - W )/2;

        let y:CGFloat = self.view.frame.size.height - W - 50;
        
        let bgImgView:UIImageView = UIImageView.init(frame: self.view.bounds)
        
        self.view.addSubview(bgImgView)
        
        bgImgView.image = UIImage.init(named: "跳球球背景2")
    
        let acView:UIActivityIndicatorView = UIActivityIndicatorView.init(frame: CGRect(x:X,y:y,width:W,height:W))
        
        bgImgView.addSubview(acView)
        
        return bgImgView
        
    }
    
    

}
